/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.bsly.modules.app.service.localmaster.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.bsly.common.exception.RRException;
import io.bsly.common.utils.PageUtils;
import io.bsly.common.validator.Assert;
import io.bsly.modules.app.dao.localDao.UserDao;
import io.bsly.modules.app.entity.localEntity.UserEntity;
import io.bsly.modules.app.pojo.LoginForm;
import io.bsly.modules.app.pojo.UserFrom;
import io.bsly.modules.app.pojo.UserMngVo;
import io.bsly.modules.app.service.localmaster.UserService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;

import java.util.List;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {

	@Override
	public UserEntity queryByMobile(String mobile) {
		return baseMapper.selectOne(new QueryWrapper<UserEntity>().eq("mobile", mobile));
	}

	@Override
	public long login(LoginForm form) {
		UserEntity user = queryByMobile(form.getMobile());
		Assert.isNull(user, "手机号或密码错误");

		//密码错误
		if(!user.getPassword().equals(DigestUtils.sha256Hex(form.getPassword()))){
			throw new RRException("手机号或密码错误");
		}
		return user.getId();
	}

    @Override
    public PageUtils queryPage(UserFrom userFrom) {
		PageHelper.startPage(userFrom.getPage(), userFrom.getLimit());
		List<UserMngVo> list=this.baseMapper.queryPageMng(userFrom);
		PageInfo<UserMngVo> pageInfo = new PageInfo(list);
		return  new PageUtils(pageInfo.getList(),(int)pageInfo.getTotal(),pageInfo.getPageSize(),pageInfo.getPageNum());
    }
}
